/**
 * 
 */
package com.wisdytech.linkdcs.system.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.linkdcs.system.dao.ISysDictDao;
import com.wisdytech.linkdcs.system.model.SysDict;
import com.wisdytech.linkdcs.system.service.ISystemDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * ClassName: SystemDictServiceImpl
 * </p>
 * <p>
 * Description: 字典业务层实现
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-13
 * </p>
 */
@Service("systemDictService")
@Transactional
public class SystemDictServiceImpl extends BaseService<ISysDictDao, SysDict> implements ISystemDictService {
    
    @Resource
    private ISysDictDao sysDictDao;
    
    @Override
    public StandardResult addSysDict(SysDict sysDict) {
        int count=0;
        count = sysDictDao.insert(sysDict);
        if (count <= 0) {
            throw new ServiceException("新增失败");
        }
          return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateSysDict(SysDict sysDict) {
        int count = 0;
        count = sysDictDao.updateById(sysDict);
        if (count <= 0) {
            throw new ServiceException("修改失败");
        }
          return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult delSysDict(String id) {
        int count;
        count = sysDictDao.deleteById(id);
        if (count <= 0) {
            throw new ServiceException("删除失败");
        }
          return StandardResult.getSuccessResult();
    }

    
    @Override
    public PageInfo<SysDict> listSysDicts(String parentKey, Filter filter) {
        EntityWrapper<SysDict> ew =new EntityWrapper<>();
        if (StringUtils.isNotBlank(parentKey)) {
            ew.like("parent_key", parentKey);
        }
        List<SysDict> lists =
                super.beforeList(ISysDictDao.class, filter).selectList(ew.eq("del_flag", CommonConstants.DEL_FLAG));
        PageInfo<SysDict> dictIdxPage=new PageInfo<>(lists);
        return dictIdxPage;
    }
    
    public SysDict getSysDict(String id ) {
        return sysDictDao.selectById(id);
    }

    @Override
    public List<SysDict> listSysDictsByParentKey(String parentKey) {
        EntityWrapper<SysDict> ew =new EntityWrapper<>();
        ew.setSqlSelect("pk_id","dict_key","dict_value");
        if (StringUtils.isNotBlank(parentKey)) {
            ew.eq("parent_key", parentKey);
            ew.orderBy("dict_sort",true);
        } else {
            return null;
        }
        List<SysDict> list =
                this.sysDictDao.selectList(ew.eq("del_flag", CommonConstants.DEL_FLAG));
        return list;
    }
}
